<?php
namespace Model;



use Core\Constant;
use Model\Factory\TableGatewayFactory;

class Dish extends AbstractModel
{
    public $id;
    public $created_date;
    public $active;

    private $_dishLangArr = array();

    /**
     * @param $lang
     * @return DishLang|null
     */
    public function getDishLang($lang){
        if(!$this->_dishLangArr[$lang]){
            $dishLangTable = TableGatewayFactory::getDishLangTable($lang);
            $this->_dishLangArr[$lang] = $dishLangTable->getByDishId($this->id);
        }

        return $this->_dishLangArr[$lang];
    }

    public function getName($lang){
        return $this->getDishLang($lang)->name;
    }

    public function getDescription($lang){
        return $this->getDishLang($lang)->description;
    }

    public function getContent($lang){
        return $this->getDishLang($lang)->content;
    }

    public function getHowTo($lang){
        return $this->getDishLang($lang)->how_to;
    }

    public function getIngredient($lang){
        return $this->getDishLang($lang)->ingredient;
    }

    public function getDishImages(){
        $dishImageTable = TableGatewayFactory::getDishImageTable();
        return $dishImageTable->getAllImageByDishId($this->id);
    }

    public function getDishPrice(){
        $dishPriceTable = TableGatewayFactory::getDishPriceTable();
        return $dishPriceTable->getAllPriceByDishId($this->id);
    }

    public function getDishCategory($lang){
        $dishCategoryTable = TableGatewayFactory::getDishCategoryTable();
        return $dishCategoryTable->getAllCategoryByDishId($this->id,$lang);
    }

    public function getDishCategoryByGroup($lang, $categoryGroup){
        $dishCategoryTable = TableGatewayFactory::getDishCategoryTable();
        return $dishCategoryTable->getAllCategoryByDishIdAndGroup($this->id,$lang, $categoryGroup);
    }

    public function getDishTags($lang){
        $dishTagTable = TableGatewayFactory::getDishTagTable($lang);
        return $dishTagTable->getAllTagsByDishId($this->id);
    }

    public function getDishTagsAsString($lang){
        $allTagRS = $this->getDishTags($lang);
        $tmpStr = '';
        if($allTagRS)
            foreach ($allTagRS as $aTag) {
                $tmpStr = $tmpStr.','.$aTag->tag;
            }
        if(strlen($tmpStr) > 0)
            $tmpStr = substr($tmpStr, 1);
        return $tmpStr;

    }

    public function getDishArticles($lang){
        $dishArticleTable = TableGatewayFactory::getDishArticleTable($lang);
        return $dishArticleTable->getAllArticleByDishId($this->id);
    }

    public function detachCategories($lang)
    {
        $dishCategoryTable = TableGatewayFactory::getDishCategoryTable();
        $allCategories = $this->getDishCategory($lang);
        if($allCategories)
            foreach ($allCategories as $aCategory) {
                $dishCategoryTable->getTableGateway()->delete(
                    array('dish_id'=>$this->id, 'category_id'=>$aCategory->id));
            }
    }

    public function detachTags($lang)
    {
        $dishTagTable = TableGatewayFactory::getDishTagTable($lang);
        $dishTagTable->getTableGateway()->delete(array('dish_id'=>$this->id,
            'lang'=>$lang));
    }

    public function detachPrices()
    {
        $dishPriceTable = TableGatewayFactory::getDishPriceTable();
        $dishPriceTable->getTableGateway()->delete(array('dish_id'=>$this->id));
    }

    public function detachArticles($lang)
    {
        $dishArticlesTable = TableGatewayFactory::getDishArticleTable($lang);
        $dishArticlesTable->getTableGateway()->delete(array('dish_id'=>$this->id,
            'lang'=>$lang));
    }

    public function detachImages()
    {
        $dishImagesTable = TableGatewayFactory::getDishImageTable();
        $dishImagesTable->getTableGateway()->delete(array('dish_id'=>$this->id));
    }

}
